package com.lawfinal.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lawfinal.entity.Lawyer;
import com.lawfinal.entity.Writ;
import com.lawfinal.service.LawyerService;
import com.lawfinal.utils.QueryPageParam;
import com.lawfinal.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KBr
 * @since 2024-04-09
 */
@RestController
@CrossOrigin("http://localhost:5173/")
@RequestMapping("/lawyer")
public class LawyerController {

      @Autowired
      private LawyerService lawyerService;

      //      登录
      @RequestMapping("/login")
      public Result lawyerLogin(@RequestBody Lawyer lawyer){
            List list=  lawyerService.lambdaQuery()
                    .eq(Lawyer::getCard,lawyer.getCard())
                    .eq(Lawyer::getPassword,lawyer.getPassword()).list();
            return list.size()>0?Result.sucess(list.get(0)):Result.fail();
      }
      //       管理员adminManager账号列表
      @RequestMapping("/list")
      public List<Lawyer> list(){
            return lawyerService.list();
      }

      //      分页基础数据
      @RequestMapping( "/listPageResult")
      public Result listPageResult(@RequestBody QueryPageParam queryPageParam){
            System.out.println("--------------------------");
//            System.out.println(queryPageParam);

            HashMap param=queryPageParam.getParam();
            System.out.println(param);
            System.out.println((String) param.get("name"));
            String name = (String) param.get("name");
            String sex = (String) param.get("sex");

            Page<Lawyer> page = new Page();
            page.setCurrent(queryPageParam.getPageNum());
            page.setSize(queryPageParam.getPageSize());

            LambdaQueryWrapper<Lawyer> lambdaQueryWrapper= new LambdaQueryWrapper();
            if (StringUtils.isNotBlank(name) && !"null".equals(name)){
                  lambdaQueryWrapper.like(Lawyer :: getName,name);
            }
            IPage result=lawyerService.page(page,lambdaQueryWrapper);

            System.out.println(result.getTotal());

            return Result.sucess(result.getRecords(),result.getTotal());
      }

      //      条件查询
//      1.列表账号查询
      @RequestMapping("findByAccount")
      public Result findByNo( String account){
            List list=lawyerService.lambdaQuery().eq(Lawyer::getCard,account).list();
            return list.size()>0?Result.sucess(list):Result.fail();
      }

      //      注册
      @RequestMapping("/enroll")
      public Result save(@RequestBody Lawyer lawyer){
            return lawyerService.save(lawyer)?Result.sucess():Result.fail();
      }

      @RequestMapping("/update")
      public Result update(@RequestBody Lawyer lawyer){
            return  lawyerService.updateById(lawyer)?Result.sucess():Result.fail();
      }


}
